package com.shujia.day16.tcp;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

public class Client {
    public static void main(String[] args) throws IOException {
        /*
            1.通过Socket类构建Socket连接
            2.准备数据，将数据发送给服务端
            3.如果服务端有数据，那么就接收数据
            4.关闭流
         */

        // 1.通过服务端的IP和端口构建socket  public Socket(String host, int port)
        Socket socket = new Socket("localhost",8888);

        // 2. 设置输出流
        OutputStream outputStream = socket.getOutputStream();
        outputStream.write("hello this is client".getBytes());
        outputStream.flush();

        socket.shutdownOutput();
        //
        InputStream inputStream = socket.getInputStream();
        byte[] bytes = new byte[1024];
        int read = inputStream.read(bytes);
        String data = new String(bytes, 0, read);
        System.out.println(data);

        // 3.关闭流
        inputStream.close();
        outputStream.close();
        socket.close();

    }
}
